package com.sunmnet.j2ee.action.powerdb.designer;

import com.sunmnet.j2ee.action.BaseNewAction;
import com.sunmnet.j2ee.bean.pub.action.JsonResult;
import com.sunmnet.j2ee.bean.pub.action.PageResult;
import com.sunmnet.j2ee.core.pub.QueryFilter;
import com.sunmnet.j2ee.core.pub.annotation.MethodName;
import com.sunmnet.j2ee.entity.powerdb.designer.Field;
import com.sunmnet.j2ee.service.BaseService;
import com.sunmnet.j2ee.service.powerdb.designer.FieldService;
import com.sunmnet.j2ee.service.powerdb.designer.IndexInfoService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 蔡岳衡 on 2016/8/8.
 */
@Controller("/powerdb/designer/field")
@RequestMapping("/powerdb/designer/field")
public class FieldAction extends BaseNewAction<Field, Long> {

	private static final Logger LOG = LogManager.getLogger(FieldAction.class.getName());

	@Resource(name = "/powerdb/designer/fieldService")
	@Override
	public void setService(BaseService<Field, Long> baseService) {
		super.service = baseService;
	}

	@Resource(name = "/powerdb/designer/indexInfoService")
	private IndexInfoService indexInfoService;

	/**
	 * 批量查询
	 *
	 * @return JsonResult
	 */

	@MethodName(name = "批量查询")
	@RequestMapping(value = "/", method = RequestMethod.PATCH)
	@ResponseBody
	public JsonResult find(HttpServletRequest request) {
		JsonResult j = new JsonResult(true);
		QueryFilter filter = new QueryFilter(request);
		try {
			j.setObj(((FieldService) service).findFields(filter));
			j.setMsg("查询成功");
		} catch (Exception var4) {
			setErrMsg("查询失败", LOG, j, var4);
		}
		return j;
	}

	/**
	 * 批量查询
	 *
	 * @param request
	 * @return JsonResult
	 */

	@MethodName(name = "批量查询")
	@RequestMapping(value = "/pages", method = RequestMethod.POST)
	@ResponseBody
	public PageResult findPages(HttpServletRequest request) {
		QueryFilter filter = new QueryFilter(request);
		return super.findPages(filter);
	}


	/**
	 * 查询
	 *
	 * @param id
	 * @return JsonResult
	 */

	@MethodName(name = "对象查询")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult get(@PathVariable("id") long id, HttpServletRequest request) {
		return super.get(id);
	}


	/**
	 * 新增
	 *
	 * @param obj
	 * @return JsonResult
	 */

	@MethodName(name = "新增")
	@RequestMapping(value = "/", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult add(Field obj) {
		if (((FieldService) service).checkRepeat(obj) == 1) {
			JsonResult jsonResult = new JsonResult(false);
			jsonResult.setObj(obj);
			jsonResult.setMsg("字段【" + obj.getName() + "】已经存在，请重新命名！");
			return jsonResult;
		}

		if (obj.getId() == null) {
			return super.add(obj);
		} else {
			/* 编辑字段时，判断是否有主键字段。
			   若有则添加进索引表
			 */
			if (1 == obj.getIsPmKey()) {
				indexInfoService.addPmKeyFieldToIndex(obj);
			}
			return super.update(obj);
		}
	}


	/**
	 * 更新
	 *
	 * @param obj
	 * @return JsonResult
	 */

	@MethodName(name = "修改")
	@RequestMapping(value = "/", method = RequestMethod.PUT)
	@ResponseBody
	public JsonResult update(Field obj) {
		if (((FieldService) service).checkRepeat(obj) == 1) {
			JsonResult jsonResult = new JsonResult(false);
			jsonResult.setObj(obj);
			jsonResult.setMsg("字段【" + obj.getName() + "】已经存在，请重新命名！");
			return jsonResult;
		}
		return super.update(obj);
	}

	/**
	 * 删除
	 *
	 * @param id
	 * @return JsonResult
	 */

	@MethodName(name = "删除")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public JsonResult delete(@PathVariable("id") long id) {
		return super.delete(id);
	}


	/**
	 * 批量删除
	 *
	 * @param ids 一个或多个id "1,2,3"
	 * @return JsonResult
	 */

	@MethodName(name = "批量删除")
	@RequestMapping(value = "/batch", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult batchDelete(@RequestParam(value = "ids", required = true) String ids) {
		return super.delete(ids);
	}

	/**
	 * 保存字段顺序
	 *
	 * @param ids 一个或多个id "1,2,3"
	 * @return
	 */
	@MethodName(name = "保存字段顺序")
	@RequestMapping(value = "/saveFieldSort", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult saveFieldSort(@RequestParam(value = "ids", required = true) String ids) {
		JsonResult jr = new JsonResult(true);
		try {
			((FieldService) service).saveFieldSort(ids);
			jr.setMsg("更新字段成功");
		}catch (Exception e){
			setErrMsg("更新字段失败",LOG,jr,e);
		}
		return jr;
	}

}
